#include "utils.h"

extern int zhhaoCropResize(
  char *src, Size srcStride, Size srcSize, Rect srcROI, 
  float *dst, Size srcStride, Size dstSize, Rect dstRoi,
  float alpha, float *beta, 
  Yuv2RgbCscParms csc, bool do_bgr2rgb, 
  void *stream=nullptr);

// JPEGD 128*16, AIPP(VPC) 16*2
int32_t main(int argc, char const *argv[])
{
  std::vector<Case> cases = {};

  const std::string src_path = "../../data/zhenzhu.jpeg";
  const Size src_size = {587, 910}, dst_size = {224, 224};
  const Rect src_roi = {17, 17, 400, 400}, dst_roi = {0, 0, 224, 224};
  const int loop = 1;
  cases.push_back({src_path, src_size, src_roi, dst_size, dst_roi, loop});

  for(size_t case_id = 0; case_id < cases.size(); ++case_id){
    Case c = cases[case_id];
    const std::string src_path = c.src_path;
    const Size src_size = c.src_size, dst_size = c.dst_size;
    const Rect src_roi = c.src_roi, dst_roi = c.dst_roi;
    const int loop = 1;

    const Size src_stride = {DIV_UP(src_size.width, 16), DIV_UP(src_size.height, 2)};
    const Size dst_stride = {DIV_UP(dst_size.width, 16), DIV_UP(dst_size.height, 2)};

    const float alpha = -2.0f, beta = 2.0f;

    CHECK(aclInit(nullptr))
    int32_t deviceId = 0;
    CHECK(aclrtSetDevice(deviceId))
    aclrtStream stream = nullptr;
    CHECK(aclrtCreateStream(&stream))
    const int src_elem_sz = src_stride.width.*src_stride.height*3/2;
    const int dst_elem_sz = dst_stride.width*dst_stride.height*3;
    char src[src_elem_sz] = {0};
    float dst[dst_elem_sz] = {0.f};
    float *d_src = nullptr;
    float *d_dst = nullptr;
    CHECK(aclrtMalloc((void**)&d_src, src_elem_sz*sizeof(char), aclrtMemMallocPolicy::ACL_MEM_MALLOC_HUGE_FIRST))
    CHECK(aclrtMalloc((void**)&d_dst, dst_elem_sz*sizeof(float), aclrtMemMallocPolicy::ACL_MEM_MALLOC_HUGE_FIRST))
    CHECK(aclrtMemcpy(d_src, src_elem_sz*sizeof(char), src, src_elem_sz*sizeof(char), aclrtMemcpyKind::ACL_MEMCPY_HOST_TO_DEVICE))
    CHECK(aclrtMemcpy(d_dst, dst_elem_sz*sizeof(char), dst, dst_elem_sz*sizeof(float), aclrtMemcpyKind::ACL_MEMCPY_HOST_TO_DEVICE))
    
    int start = now();
    for(int i = 0; i < loop; ++i){
      zhhaoCropResize(
        d_src, src_stride, src_size, src_roi, 
        d_dst, dst_stride, dst_size, dst_roi,
        alpha, beta, 
        c.csc, false, 
        stream);
    }
    CHECK(aclrtSynchronizeStream(stream))
    int end = now();

    std::string func_name = "zhhaoBitGemm";
    cases[case_id].zhhao_qps = float(loop)/(end-start)*1000;
    dumpbin(d_dst, dst_elem_sz*sizeof(float), func_name + ".bin");
    CHECK(aclrtDestroyStream(stream))
    CHECK(aclrtResetDevice(deviceId))
    CHECK(aclFinalize())
  }
  return 0;
}
